package com.leve.control.controller;


import com.leve.control.constant.Constants;
import com.leve.control.constant.ErrorCode;
import com.leve.control.domain.Live;
import com.leve.control.service.LiveService;
import com.leve.control.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/api/live")
@Api(tags = {"APP端直播相关接口服务"})
public class LeveLiveController {

    @Autowired
    private LiveService liveService;

    @ApiOperation(value = "设备直播相关接口")
    @PostMapping(value = "/start")
    @ResponseBody
    public Result startLive(@RequestParam String deviceId){
        log.info(Constants.REQ + ErrorCode.SUCCESS.getCode() + "|api/live/start|设备直播相关接口,deviceId={}: "+ deviceId);
        try{
            Live live = liveService.startLive(deviceId);
            return new Result(ErrorCode.SUCCESS,live);
        }catch (Exception e){
            log.error(Constants.RES + ErrorCode.E_10001.getCode()+ "|api/live/start|APP端设备直播相关接口", e);
            return new Result(ErrorCode.E_10001);
        }
    }

    @ApiOperation(value = "获取设备直播相关信息接口")
    @GetMapping(value = "/get")
    @ResponseBody
    public Result getLive(@RequestParam String deviceId){
        log.info(Constants.REQ + ErrorCode.SUCCESS.getCode() + "|api/live/get|获取设备直播相关信息接口,deviceId={}: "+deviceId);
        try{
            Live live = liveService.getLiveInfo(deviceId);
            return new Result(ErrorCode.SUCCESS,live);
        }catch (Exception e){
            log.error(Constants.RES + ErrorCode.E_10001.getCode()+ "|api/live/get|获取设备直播相关信息接口", e);
            return new Result(ErrorCode.E_10001);
        }
    }

}
